package com.cvit.weixin;
import java.io.StringWriter;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



public class WXXmlSp {
	/**
	 * 微信统一下单输出XML
	 * @author wjp
	 */
	public static String getXmlOrder(Map<String,Object> map) {
		StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
		sb.append("<appid>"+map.get("appid")+"</appid>");  //微信分配的公众号ID
		sb.append("<mch_id>"+map.get("mch_id")+"</mch_id>");  	//微信支付分配的商户号ID
//		sb.append("<sub_appid>"+map.get("sub_appid")+"</sub_appid>");  //微信分配的公众号ID
		sb.append("<sub_mch_id>"+map.get("sub_mch_id")+"</sub_mch_id>");  	//微信支付分配的商户号ID
		sb.append("<device_info>"+map.get("device_info")+"</device_info>");
		sb.append("<nonce_str>"+map.get("nonce_str")+"</nonce_str>");
		sb.append("<sign_type>"+map.get("sign_type")+"</sign_type>");
		sb.append("<fee_type>"+map.get("fee_type")+"</fee_type>");
		sb.append("<body>"+"<![CDATA["+map.get("body")+"]]>"+"</body>");//"<"+k+">"+"<![CDATA["+v+"]]></"+k+">"
		sb.append("<out_trade_no>"+map.get("out_trade_no")+"</out_trade_no>");
		sb.append("<total_fee>"+map.get("total_fee")+"</total_fee>");
		sb.append("<spbill_create_ip>"+map.get("spbill_create_ip")+"</spbill_create_ip>");
		sb.append("<notify_url>"+map.get("notify_url")+"</notify_url>"); //接收微信支付异步通知回调地址		 
		sb.append("<trade_type>"+map.get("trade_type")+"</trade_type>");
//		sb.append("<product_id>"+map.get("product_id")+"</product_id>");
		sb.append("<limit_pay>"+map.get("limit_pay")+"</limit_pay>");
//		sb.append("<time_start>"+map.get("time_start")+"</time_start>");
//		sb.append("<time_expire>"+map.get("time_expire")+"</time_expire>");
//		sb.append("<sub_openid>"+map.get("sub_openid")+"</sub_openid>");
		sb.append("<openid>"+map.get("openid")+"</openid>");
		sb.append("<sign>"+map.get("sign")+"</sign>");
		sb.append("</xml>");
        return sb.toString();
    }
	
	/**
	 * 微信统一下单输出XML
	 * @author wjp
	 */
	public static String getCloseXmlOrder(Map<String,Object> map) {
		StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
		sb.append("<appid>"+map.get("appid")+"</appid>");  //微信分配的公众号ID
		sb.append("<mch_id>"+map.get("mch_id")+"</mch_id>");  	//微信支付分配的商户号ID
//		sb.append("<sub_appid>"+map.get("sub_appid")+"</sub_appid>");  //微信分配的公众号ID
		sb.append("<sub_mch_id>"+map.get("sub_mch_id")+"</sub_mch_id>");  	//微信支付分配的商户号ID
		sb.append("<nonce_str>"+map.get("nonce_str")+"</nonce_str>");
		sb.append("<out_trade_no>"+map.get("out_trade_no")+"</out_trade_no>");
		sb.append("<sign>"+map.get("sign")+"</sign>");
		sb.append("</xml>");
        return sb.toString();
    }
	
	/**
     * 将Map转换为XML格式的字符串
     *
     * @param data Map类型数据
     * @return XML格式的字符串
     * @throws Exception
     */
    public static String mapToXml(Map<String, String> data) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
        org.w3c.dom.Document document = documentBuilder.newDocument();
        org.w3c.dom.Element root = document.createElement("xml");
        document.appendChild(root);
        for (String key: data.keySet()) {
            String value = data.get(key);
            if (value == null) {
                value = "";
            }
            value = value.trim();
            org.w3c.dom.Element filed = document.createElement(key);
            filed.appendChild(document.createTextNode(value));
            root.appendChild(filed);
        }
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
        try {
            writer.close();
        }
        catch (Exception ex) {
        }
        return output;
    }
	
	/**
	 * 微信回调输出XML
	 * @author wjp
	 */
	public static String getXmlOrderResult(String code, String msg) {
		StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
		sb.append("<return_code>"+code+"</return_code>");
		sb.append("<return_msg>"+msg+"</return_msg>");
		sb.append("</xml>");
		//System.out.println(sb.toString());
        return sb.toString();
    }
}
